package com.stylefeng.guns.rest.modular.cinema;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceAPI;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.api.order.OrderServiceAPI;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaConditionResponseVO;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaFieldResponseVO;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaFieldsResponseVO;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaListResponseVO;
import com.stylefeng.guns.rest.modular.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CinemaController
 * Created by zghgchao 2019/8/20 21:05
 **/
@Slf4j
@RestController
@RequestMapping("/cinema")
public class CinemaController {

    // 关闭 启动时检查 dubbo服务是否开启
    @Reference(interfaceClass = CinemaServiceAPI.class,cache = "lru",check = false,connections = 10)
    private CinemaServiceAPI cinemaServiceAPI;

    @Reference(interfaceClass = OrderServiceAPI.class,check = false)
    private OrderServiceAPI orderServiceAPI;

    private static final String IMG_PRE = "http://img.meetingshop.cn";

    /**
     * 查询影院列表-根据条件查询所有影院
     * @param cinemaQueryVO
     * @return
     */
    @GetMapping("/getCinemas")
    public ResponseVO getCinemas(CinemaQueryVO cinemaQueryVO){

        try {
            // 按照五个条件进行筛选
            Page<CinemaVO> cinemaVOPage = cinemaServiceAPI.getCinemas(cinemaQueryVO);

            // 判断是否有满足条件的影院
            if (cinemaVOPage.getRecords() == null || cinemaVOPage.getSize() == 0){
                return ResponseVO.success("没有影院可查！");
            }else {
                return ResponseVO.success(cinemaVOPage.getCurrent(), (int) cinemaVOPage.getTotal(),"",new CinemaListResponseVO(cinemaVOPage.getRecords()));
            }
        }catch (Exception e){
            // 如果出现异常，应该如何处理
            log.error("获取影院列表异常：{}",e);
            log.error("获取影院列表异常：{}",e.getMessage());
            return ResponseVO.serviceFail("查询影院列表失败！");
        }


    }

    /**
     * 获取影院列表查询条件
     * @param cinemaQueryVO   1、热点数据 -> 放缓存 1、banner
     * @return
     */
    @GetMapping("/getCondition")
    public ResponseVO getCondition(CinemaQueryVO cinemaQueryVO){

        try {
            // 获取三个集合，然后封装成一个对象返回即可
            List<BrandVO> brands = cinemaServiceAPI.getBrands(cinemaQueryVO.getBrandId());
            List<AreaVO> areas = cinemaServiceAPI.getAreas(cinemaQueryVO.getDistrictId());
            List<HallTypeVO> hallTypes = cinemaServiceAPI.getHallTypes(cinemaQueryVO.getHallType());

            CinemaConditionResponseVO cinemaConditionResponseVO = new CinemaConditionResponseVO();
            cinemaConditionResponseVO.setBrandList(brands);
            cinemaConditionResponseVO.setAreaList(areas);
            cinemaConditionResponseVO.setHallTypeList(hallTypes);

            return ResponseVO.success(cinemaConditionResponseVO);
        }catch (Exception e){
            log.error("获取条件列表失败：{}",e);
            return ResponseVO.serviceFail("获取条件列表失败!");
        }
    }

    /**
     * 获取播放场次接口
     * @param cinemaId
     * @return
     */
    @GetMapping("/getFields")
    public ResponseVO getFields(@RequestParam("cinemaId") Integer cinemaId){
        try {
            CinemaInfoVO cinemaInfoVO = cinemaServiceAPI.getCinemaInfoByCinemaId(cinemaId);

            List<FilmInfoVO> filmInfoVOList = cinemaServiceAPI.getFilmInfoByCinemaId(cinemaId);

            CinemaFieldsResponseVO cinemaFieldsResponseVO = new CinemaFieldsResponseVO();
            cinemaFieldsResponseVO.setCinemaInfo(cinemaInfoVO);
            cinemaFieldsResponseVO.setFilmList(filmInfoVOList);

            return ResponseVO.success(IMG_PRE,cinemaFieldsResponseVO);
        }catch (Exception e){
            log.error("获取播放场次失败：{}",e);
            return ResponseVO.serviceFail("获取播放场次失败!");
        }
    }

    /**
     * 获取场次详细信息接口
     * @param cinemaId
     * @param fieldId
     * @return
     */
    @GetMapping("/getFieldInfo")
    public ResponseVO getFieldInfo(@RequestParam("cinemaId") Integer cinemaId,@RequestParam("fieldId") Integer fieldId){
        try {
            CinemaInfoVO cinemaInfoVO = cinemaServiceAPI.getCinemaInfoByCinemaId(cinemaId);
            FilmInfoVO filmInfoVO = cinemaServiceAPI.getFilmInfoByFieldId(fieldId);
            HallInfoVO filmFieldInfo = cinemaServiceAPI.getFilmFieldInfo(fieldId);

            // 造几个销售的假数据，后续会对接订单接口
            filmFieldInfo.setSoldSeats(orderServiceAPI.getSoldSeatsByFieldId(fieldId));

            CinemaFieldResponseVO cinemaFieldResponseVO = new CinemaFieldResponseVO();
            cinemaFieldResponseVO.setCinemaInfo(cinemaInfoVO);
            cinemaFieldResponseVO.setFilmInfo(filmInfoVO);
            cinemaFieldResponseVO.setHallInfo(filmFieldInfo);

            return ResponseVO.success(IMG_PRE,cinemaFieldResponseVO);
        }catch (Exception e){
            log.error("获取选座信息失败：{}",e);
            return ResponseVO.serviceFail("获取选座信息失败!");
        }
    }
}
